<?php


namespace app\admin\model;


use think\Model;

/**
 * Class HtnsOrderRefund
 * @package app\admin\model
 * @desc 海淘女神-退款订单
 */
class HtnsOrderRefund extends Model
{
    protected $connection = 'htns';
    protected $table = 'eb_store_order_refund';

    // 获取售后状态列表
    public function getRefundStatus($id = 0)
    {
        $status = [
            -1 => '暂无售后',
            1 => '仅退款',
            2 => '退货退款',
            3 => '拒绝退款',
            4 => '商品待退货',
            5 => '退货待收货',
            6 => '已退款',
        ];
        if($id){
            $status = $status[$id];
        }
        return $status;
    }

    // 获取订单售后状态
    public function getOrderRefundStatus($oid = 0 , $id = 0)
    {
        $status                  = '暂无售后';
        if($oid){
            $where['store_order_id'] = $oid;
        }elseif ($id){
            $where['id'] = $id;
        }
        $res                     = self::where($where)->order('id desc')->find();
        if ($res) {
            // 拒绝退款
            if ($res['refund_type'] == 1) {
                $status = '仅退款';
            } elseif ($res['refund_type'] == 2) {
                $status = '退货退款';
            } elseif ($res['refund_type'] == 3) {
                $status = '拒绝退款';
                $status .= '<br/>原因:'.$res['refuse_reason'];
            } elseif ($res['refund_type'] == 4) {
                $status = '商品待退货';
            } elseif ($res['refund_type'] == 5) {
                $status = '退货待收货';
                if ($res['refund_express']) {
                    $status .= ',退货运单号:' . $res['refund_express'];
                }
            } elseif ($res['refund_type'] == 6) {
                $status = '已退款';
            }
        }
        return $status;
    }

    // 获取当前退款每个状态值的进度
    public function getRefundSchedule($refund_type)
    {
        $row['refundAgree'] = 0;
        $row['refundSuc'] = 0;
        if($refund_type > 3){
            $row['refundAgree'] = 1;
        }
        if($refund_type == 6){
            $row['refundSuc'] = 1;
        }
        return $row;
    }

    // 获取待退款订单数
    public function getWaitRefund()
    {
        $whereArr[] = ['refund_type', 'in', [1,2,4,5]];
        $num = self::where($whereArr)->count();
        return $num;
    }
    // 获取退款成功订单数
    public function getRefundSucNum()
    {
        $whereArr[] = ['refund_type', '=', 6];
        $num = self::where($whereArr)->count();
        return $num;
    }
    // 获取拒绝退款订单数
    public function getRefuseRefundNum()
    {
        $whereArr[] = ['refund_type', '=', 3];
        $num = self::where($whereArr)->count();
        return $num;
    }
    // 获取退款失败订单数
    public function getRefundFailNum()
    {
        return 0;
    }
}